package com.qu.visitmodel;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import com.qu.visitmodel.service.RecordService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class MainActivity extends AppCompatActivity {

    private FloatingActionMenu recordButton;

    private FloatingActionButton playButton;
    private FloatingActionButton pauseButton;
    private FloatingActionButton stopButton;

    private TextView tv_record_time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        EventBus.getDefault().register(this);
    }

    private void initView() {
        recordButton = (FloatingActionMenu) findViewById(R.id.record);

        playButton = (FloatingActionButton) findViewById(R.id.play);
        pauseButton = (FloatingActionButton) findViewById(R.id.pause);
        stopButton = (FloatingActionButton) findViewById(R.id.stop);

        tv_record_time = (TextView) findViewById(R.id.tv_record_time);

        playButton.setOnClickListener(clickListener);
        pauseButton.setOnClickListener(clickListener);
        stopButton.setOnClickListener(clickListener);

        recordButton.setClosedOnTouchOutside(true);

    }



    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.play:
                    Toast.makeText(MainActivity.this, "Play", Toast.LENGTH_SHORT).show();

                    Intent startIntent = new Intent(MainActivity.this, RecordService.class);
                    startService(startIntent);
                    break;
                case R.id.pause:
                    Toast.makeText(MainActivity.this, "Pause", Toast.LENGTH_SHORT).show();
                    Intent pauseIntent = new Intent(MainActivity.this, RecordService.class);
                    stopService(pauseIntent);
                    break;
                case R.id.stop:
                    Toast.makeText(MainActivity.this, "Stop", Toast.LENGTH_SHORT).show();
                    Intent stopIntent = new Intent(MainActivity.this, RecordService.class);
                    stopService(stopIntent);
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent pauseIntent = new Intent(MainActivity.this, RecordService.class);
        stopService(pauseIntent);

        EventBus.getDefault().unregister(this);
    }

    public void next(View view) {
        startActivity(new Intent(this, SecondActivity.class));
    }

    /**
     *
     * @param state 当前状态
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Integer state) {
        Log.d("MainActivity", "MainActivity" + state);

    }

    /**
     *
     * @param time 当前的录制时间
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeReceive(String time) {
        tv_record_time.setText(time);
        Log.d("MainActivity", "MainActivity" + time);
    }

}
